/*
 * Copyright (c) 2015 Minshenglife. All rights reserved.
 * Minshenglife PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.sentry.jobs;

import org.apache.commons.lang3.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author WuBo (wub1@minshenglife.com)
 *
 */
public class HTTPRequestHeaders {
    
    
    private static final String REQ_RAW_TPL = 
            "%s %s HTTP/1.1\r\n" + 
            "Host: %s\r\n" + 
            "Connection: %s\r\n" + 
            "User-Agent: %s\r\n" +
            "\r\n"; 
            
    
    
    private String method = "GET";
    
    private String path = "/";
    
    private String host;
    
    private String connection = "close";
    
    private String userAgent = "Mozilla/5.0 (Windows NT 6.1)";
    
    
    public HTTPRequestHeaders(URL url) {
        this.path = url.getPath();
        if(!StringUtils.isEmpty(url.getQuery())){
            this.path = this.path + "?"+url.getQuery();
        }
        this.host = url.getHost();
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format(REQ_RAW_TPL, this.method, this.path, this.host,this.connection,this.userAgent);
    }
    
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("https://www.baidu.com/s?wd=it&rsv_spt=1&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=3&rsv_sug1=2&rsv_sug2=0&inputT=283&rsv_sug4=1556");
        HTTPRequestHeaders h = new HTTPRequestHeaders(url);
        System.out.println(h.toString());
    }
}
